package game.Sprites;

import game.Player.Player;
import game.Utils.GraphicsUtils;
import game.Vector3;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.effect.BlendMode;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;


import java.awt.geom.Rectangle2D;

public class VanishingSprite extends Sprite {
    protected double size;
    protected double opacity;
    protected Image img;
    protected final long initTime, destroyTime;

    public VanishingSprite(Vector3 pos, double rotation, double size, Image img, Color color, double vanishingTime) throws Exception {
        super(pos, rotation, color);
        this.initTime = System.currentTimeMillis();
        this.destroyTime = System.currentTimeMillis() + (long)(vanishingTime*1000L);
        this.img = img;
        this.size = size;
        this.opacity = 1.0;
        this.destroy(vanishingTime);
    }

    @Override
    public void draw(GraphicsContext gd, Vector3 screenPos, double squareRes) {
        // Tint effect and opacity
        ColorAdjust colorAdjust = new ColorAdjust();
        colorAdjust.setHue(color.getHue());
        colorAdjust.setSaturation(color.getSaturation());
        colorAdjust.setBrightness(opacity-1.0);
        gd.setEffect(colorAdjust);
        gd.setGlobalAlpha(opacity);
        gd.setGlobalBlendMode(BlendMode.MULTIPLY);

        // Draw image
        //gd.drawImage(img,
        //        (int) screenPos.x - (int) (squareRes * size / 2),(int) screenPos.y - (int) (squareRes * size / 2),
        //        (int) (squareRes * size), (int) (squareRes * size));
        GraphicsUtils.drawRotatedImage(gd, img, rotation,
                 (int) screenPos.x - (int) (squareRes * size / 2),(int) screenPos.y - (int) (squareRes * size / 2),
                    (int) (squareRes * size), (int) (squareRes * size));

        // Reset effects
        gd.setEffect(null);
        gd.setGlobalBlendMode(BlendMode.SRC_OVER);
        gd.setGlobalAlpha(1.0);
    }

    @Override
    public Rectangle2D.Double getBound() {
        return new Rectangle2D.Double(this.position.x - size/2, this.position.y - size/2, size, size);
    }

    @Override
    public void update(double deltaTime) {
        if(Player.myPlayer != null && Player.myPlayer.freeze){
            destroy();
        }
        this.opacity = 1.0 - (double)(System.currentTimeMillis() - initTime) / (destroyTime - initTime);
    }
}
